local tongyan = fk.CreateSkill {
  name = "xiaobai__tongyan"
}

Fk:loadTranslationTable {
  ["xiaobai__tongyan"] = "通言",
  [":xiaobai__tongyan"] = "你可以将包含你在内的任意名角色共三张手牌当一张不可响应的【无懈可击】使用，" ..
      "若其中点数最大的不为你的牌，此技能本轮失效。",
  ["#xiaobai__tongyan"] = "通言：你可以将包含你在内的任意名角色共三张手牌当【无懈可击】使用,请选择你要转化的牌！",
  ["#xiaobai__tongyan_invalid"] = "通言：此技能本轮失效",

  ["#tongyan-choose"] = "通言：请选择你要转化的其他角色的牌",
}

tongyan:addEffect("viewas", {
  prompt = "#xiaobai__tongyan",
  anim_type = "defensive",
  pattern = "nullification",
  card_filter = function(self, player, to_select, selected)
    -- 只能选择手牌，且总共只能选择3张
    if #selected >= 3 then return false end
    return true
  end,

  view_as = function(self, player, cards)
    if #cards > 3 then return end

    local nullification = Fk:cloneCard("nullification")
    nullification:addSubcards(cards)
    nullification.skillName = tongyan.name
    return nullification
  end,
  before_use = function(self, player, use)
    local room = player.room
    -- 弃置三张手牌
    local targets = room.alive_players
    local n = #use.card.subcards
    while n < 3 do
      local tos = room:askToChoosePlayers(player, {
        targets = targets,
        prompt = "#tongyan-choose",
        skill_name = "xiaobai__tongyan",
        min_num = 1,
        max_num = 1,
      })
      if #tos == 0 then
        break
      end
      local cards = room:askToChooseCards(player, {
        target = tos[1],
        min = 1,
        max = 3 - n,
        flag = "h",
        skill_name = tongyan.name,
      })
      if #cards > 0 then
        use.card:addSubcards(cards)
        n = n + #cards
      end
    end
    use.disresponsiveList = table.simpleClone(player.room.players)
    --获取最大牌
    local max = 0
    local max_cid
    local playercards = player:getCardIds("h")
    for _, cid in ipairs(use.card.subcards) do
      local card = Fk:getCardById(cid)
      if card.number > max then
        max = card.number
        max_cid = cid
      end
    end
    if not table.contains(playercards, max_cid) then
      room:invalidateSkill(player, tongyan.name, "-round")
    end
  end,
})

return tongyan